package cn.edu.hpu.action;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hpu.pojo.Deal;
import cn.edu.hpu.pojo.Message;
import cn.edu.hpu.pojo.Task;
import cn.edu.hpu.pojo.User;
import cn.edu.hpu.service.ContactsService;
import cn.edu.hpu.service.DealService;
import cn.edu.hpu.service.MessageService;
import cn.edu.hpu.service.TaskService;
import cn.edu.hpu.util.DateUtil;
import cn.edu.hpu.util.Pager;
import cn.edu.hpu.util.TimeUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MessageAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private MessageService messageService;
	private List<Message> mlist;
	private String ctime;
	private List<Task> tlist;
	private List<Task> tlist1;
	private TaskService taskService;
	private String dtime;
	private long ccount;
	private long tcount;
	private long dcount;
	private List<Deal> dlist;
	private ContactsService contactsService;
	private DealService dealService;
	private String id;
	private Pager<Message> pager;
	private int p;
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String findAllMessage(){
		try {

			int num=1;
			if (p >=1) {
				num=p;
			}else{
				p = 1;
				num=1;
			}
			pager = messageService.findByPager(num);
			System.out.println(pager.getTotalcount()+","+pager.getTotalpages());
			mlist = pager.getResult();
			//mlist = messageService.findAllMessage();
			tlist = taskService.findByProperty("complete",false);
			tlist1 = taskService.findByProperty("complete", true);
			ctime = TimeUtil.getTime();
			dtime = DateUtil.getTime();
			Map<String,Object> map = ActionContext.getContext().getSession();
			User user =  (User) map.get("user");
			ccount = contactsService.ccount(user.getId());
			tcount = taskService.tcount(user.getId());
			dcount = dealService.dcount(user.getId());
			map.put("ccount", ccount);
			map.put("tcount", tcount);
			map.put("dcount", dcount);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		
	}
	
	public String update(){
		Message message = messageService.findA(id);
		message.setEnable(false);
		messageService.update(message);
		return SUCCESS;
	}
	
	
	
	
	
	//setter,getter
	
	@Autowired
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	
	
	public Pager<Message> getPager() {
		return pager;
	}

	public void setPager(Pager<Message> pager) {
		this.pager = pager;
	}

	public int getP() {
		return p;
	}

	public void setP(int p) {
		this.p = p;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setContactsService(ContactsService contactsService) {
		this.contactsService = contactsService;
	}
	@Autowired
	public void setDealService(DealService dealService) {
		this.dealService = dealService;
	}

	public List<Deal> getDlist() {
		return dlist;
	}

	public void setDlist(List<Deal> dlist) {
		this.dlist = dlist;
	}

	public void setCcount(long ccount) {
		this.ccount = ccount;
	}
	public long getTcount() {
		return tcount;
	}

	public void setTcount(long tcount) {
		this.tcount = tcount;
	}

	public long getDcount() {
		return dcount;
	}

	public void setDcount(long dcount) {
		this.dcount = dcount;
	}

	public String getDtime() {
		return dtime;
	}

	public void setDtime(String dtime) {
		this.dtime = dtime;
	}

	public List<Task> getTlist1() {
		return tlist1;
	}

	public void setTlist1(List<Task> tlist1) {
		this.tlist1 = tlist1;
	}

	public List<Task> getTlist() {
		return tlist;
	}

	public void setTlist(List<Task> tlist) {
		this.tlist = tlist;
	}
	@Autowired
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public String getCtime() {
		return ctime;
	}

	public void setCtime(String ctime) {
		this.ctime = ctime;
	}

	public List<Message> getMlist() {
		return mlist;
	}

	public void setMlist(List<Message> mlist) {
		this.mlist = mlist;
	}

}
